package com.uwlaser.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import com.uwlaser.base.PageInfo;
import com.uwlaser.base.QueryHelpMybatisPlus;
import com.uwlaser.base.impl.CommonServiceImpl;
import com.uwlaser.utils.ConvertUtil;
import com.uwlaser.utils.PageUtil;
import com.uwlaser.domain.DeviceAttribute;
import com.uwlaser.service.DeviceAttributeService;
import com.uwlaser.service.dto.DeviceAttributeDto;
import com.uwlaser.service.dto.DeviceAttributeQueryParam;
import com.uwlaser.service.mapper.DeviceAttributeMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Pageable;
import java.util.*;

/**
* @author zk-Qin
* @date 2022-05-11
*/
@Service
@AllArgsConstructor
// @CacheConfig(cacheNames = DeviceAttributeService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class DeviceAttributeServiceImpl extends CommonServiceImpl<DeviceAttributeMapper, DeviceAttribute> implements DeviceAttributeService {

    // private final RedisUtils redisUtils;
    private final DeviceAttributeMapper deviceAttributeMapper;

    @Override
    public PageInfo<DeviceAttributeDto> queryAll(DeviceAttributeQueryParam query, Pageable pageable) {
        IPage<DeviceAttribute> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<DeviceAttribute> page = deviceAttributeMapper.selectPage(queryPage, QueryHelpMybatisPlus.getPredicate(query));
        return ConvertUtil.convertPage(page, DeviceAttributeDto.class);
    }

    @Override
    public List<DeviceAttributeDto> queryAll(DeviceAttributeQueryParam query){
        return ConvertUtil.convertList(deviceAttributeMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), DeviceAttributeDto.class);
    }

    @Override
    public DeviceAttribute getById(Integer id) {
        return deviceAttributeMapper.selectById(id);
    }

    @Override
    // @Cacheable(key = "'id:' + #p0")
    public DeviceAttributeDto findById(Integer id) {
        return ConvertUtil.convert(getById(id), DeviceAttributeDto.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(DeviceAttributeDto resources) {
        DeviceAttribute entity = ConvertUtil.convert(resources, DeviceAttribute.class);
        return deviceAttributeMapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateById(DeviceAttributeDto resources){
        DeviceAttribute entity = ConvertUtil.convert(resources, DeviceAttribute.class);
        int ret = deviceAttributeMapper.updateById(entity);
        // delCaches(resources.id);
        return ret;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeByIds(Set<Integer> ids){
        // delCaches(ids);
        return deviceAttributeMapper.deleteBatchIds(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeById(Integer id){
        Set<Integer> set = new HashSet<>(1);
        set.add(id);
        return this.removeByIds(set);
    }

    /*
    private void delCaches(Integer id) {
        redisUtils.delByKey(CACHE_KEY + "::id:", id);
    }

    private void delCaches(Set<Integer> ids) {
        for (Integer id: ids) {
            delCaches(id);
        }
    }*/

    /*
    @Override
    public void download(List<DeviceAttributeDto> all, HttpServletResponse response) throws IOException {
      List<Map<String, Object>> list = new ArrayList<>();
      for (DeviceAttributeDto deviceAttribute : all) {
        Map<String,Object> map = new LinkedHashMap<>();
              map.put("父ID", deviceAttribute.getPid());
              map.put("属性编码", deviceAttribute.getAttrCode());
              map.put("属性名称", deviceAttribute.getAttrName());
              map.put("属性值", deviceAttribute.getAttrValue());
              map.put("描述", deviceAttribute.getRemark());
              map.put("创建人", deviceAttribute.getCreateBy());
              map.put("创建时间", deviceAttribute.getCreateTime());
              map.put("更新人", deviceAttribute.getUpdateBy());
              map.put("更新时间", deviceAttribute.getUpdateTime());
        list.add(map);
      }
      FileUtil.downloadExcel(list, response);
    }*/
}
